const { DataTypes } = require("sequelize");
const { sequelize } = require("../dbConnect");

const Course = sequelize.define('courses', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    comment: "课程ID"
  },
  title: {
    type: DataTypes.STRING(200),
    allowNull: false,
    comment: "课程标题"
  },
  description: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: "课程描述"
  },
  cover: {
    type: DataTypes.STRING(500),
    allowNull: true,
    comment: "封面图片"
  },
  category_id: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: "分类ID"
  },
  instructor: {
    type: DataTypes.STRING(100),
    allowNull: true,
    comment: "讲师"
  },
  difficulty: {
    type: DataTypes.ENUM('beginner', 'intermediate', 'advanced'),
    defaultValue: 'beginner',
    comment: "难度级别"
  },
  duration: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: "课程时长(秒)"
  },
  price: {
    type: DataTypes.DECIMAL(10, 2),
    defaultValue: 0.00,
    comment: "价格"
  },
  original_price: {
    type: DataTypes.DECIMAL(10, 2),
    defaultValue: 0.00,
    comment: "原价"
  },
  rating: {
    type: DataTypes.DECIMAL(3, 2),
    defaultValue: 0.00,
    comment: "评分"
  },
  student_count: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: "学生数量"
  },
  lesson_count: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: "课时数量"
  },
  tags: {
    type: DataTypes.JSON,
    allowNull: true,
    comment: "标签"
  },
  content_url: {
    type: DataTypes.STRING(500),
    allowNull: true,
    comment: "内容链接"
  },
  is_featured: {
    type: DataTypes.BOOLEAN,
    defaultValue: false,
    comment: "是否精选"
  },
  is_active: {
    type: DataTypes.BOOLEAN,
    defaultValue: true,
    comment: "是否启用"
  }
}, {
  tableName: 'courses',
  indexes: [
    {
      fields: ['category_id']
    }
  ]
});

const CourseCategory = sequelize.define('course_categories', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    comment: "分类ID"
  },
  name: {
    type: DataTypes.STRING(100),
    allowNull: false,
    comment: "分类名称"
  },
  description: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: "分类描述"
  },
  icon: {
    type: DataTypes.STRING(100),
    allowNull: true,
    comment: "图标"
  },
  color: {
    type: DataTypes.STRING(20),
    allowNull: true,
    comment: "颜色"
  },
  sort: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: "排序"
  },
  is_active: {
    type: DataTypes.BOOLEAN,
    defaultValue: true,
    comment: "是否启用"
  }
}, {
  tableName: 'course_categories'
});

// 用户课程进度表
const UserCourseProgress = sequelize.define('user_course_progress', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    comment: "课程进度ID"
  },
  user_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: "用户ID"
  },
  course_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: "课程ID"
  },
  progress: {
    type: DataTypes.DECIMAL(5, 2),
    defaultValue: 0.00,
    comment: "进度百分比"
  },
  current_lesson: {
    type: DataTypes.INTEGER,
    defaultValue: 1,
    comment: "当前课时"
  },
  total_lessons: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: "总课时数"
  },
  study_time: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: "学习时长(秒)"
  },
  last_study_at: {
    type: DataTypes.DATE,
    allowNull: true,
    comment: "最后学习时间"
  },
  is_completed: {
    type: DataTypes.BOOLEAN,
    defaultValue: false,
    comment: "是否完成"
  },
  completed_at: {
    type: DataTypes.DATE,
    allowNull: true,
    comment: "完成时间"
  },
  rating: {
    type: DataTypes.DECIMAL(3, 2),
    allowNull: true,
    comment: "用户评分"
  },
  review: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: "用户评价"
  }
}, {
  tableName: 'user_course_progress',
  indexes: [
    {
      unique: true,
      fields: ['user_id', 'course_id']
    }
  ]
});

// 定义关联关系
Course.belongsTo(CourseCategory, {
  foreignKey: 'category_id',
  as: 'category'
});

CourseCategory.hasMany(Course, {
  foreignKey: 'category_id',
  as: 'courses'
});

module.exports = {
  Course,
  CourseCategory,
  UserCourseProgress
};
